// SettingTCPIPDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DSTapeMount.h"
#include "SettingTCPIPDlg.h"
#include "DSTapeMountDoc.h"
#include "MainFrm.h"
#include <assert.h>
#include "OnLine/COnLine.h"
#include "uLoaderPort/ILoaderPort.h"
#include "DataManager/CDMControlCenter.h"
#include "AutoManager/IAutoManager.h"
#include "TrsLoaderPort/ITrsLoaderPort.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSettingTCPIPDlg dialog
CSettingTCPIPDlg::CSettingTCPIPDlg(IAutoManager* pAM,CDMControlCenter* pDMCC,IOnLine* pOnLine,CWnd* pParent /*=NULL*/)
	: CDialog(CSettingTCPIPDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSettingTCPIPDlg)
	m_eCommLog = _T("");
	m_fSendOK = FALSE;
	m_Msg = _T("");
	m_ServerStatus = _T("");
	//}}AFX_DATA_INIT
	assert(pAM != NULL && pOnLine != NULL);
	m_lnkOnLine = pOnLine;
	m_lnkSystemData = pDMCC->GetSystemDataCOM();
	m_lnkLoaderPort = pAM->GetTrsLoaderPortCOM()->GetLoaderPortCOM();
	strEqid = m_lnkSystemData->GetEQID();
}


void CSettingTCPIPDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSettingTCPIPDlg)
	DDX_Control(pDX, IDB_AGV_ON_OK, m_bAgvOn);
	DDX_Control(pDX, IDB_AGV_OFF_OK, m_bAgvOff);
	DDX_Control(pDX, IDB_PORT_UNLOADING_OK, m_bPortUnloading);
	DDX_Control(pDX, IDB_PORT_LOADING_OK, m_bPortLoading);
	DDX_Control(pDX, IDB_BCR_READ_OK, m_bBcrRead);
	DDX_Control(pDX, IDB_READY_TO_WORK_OK, m_bReadToWork);
	DDX_Control(pDX, IDB_WAFER_WORK_START_OK, m_bWaferWorkStart);
	DDX_Control(pDX, IDB_WAFER_WORK_COMPLETE_OK, m_bWaferWorkComplete);
	DDX_Control(pDX, IDB_WORK_COMPLETE_OK, m_bWorkComplete);
	DDX_Control(pDX, IDB_ALARM_ON_OK, m_bAlarmOn);
	DDX_Control(pDX, IDB_ALARM_OFF_OK, m_bAlarmOff);
	DDX_Control(pDX, IDB_EXIT, m_bExit);
	DDX_Text(pDX, IDC_STATIC_SERVER_STATUS, m_ServerStatus);
	//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSettingTCPIPDlg, CDialog)
	//{{AFX_MSG_MAP(CSettingTCPIPDlg)
	ON_BN_CLICKED(IDB_EXIT, OnExit)
	ON_BN_CLICKED(IDB_AGV_ON_OK, OnAgvOnOk)
	ON_BN_CLICKED(IDB_AGV_OFF_OK, OnAgvOffOk)
	ON_BN_CLICKED(IDB_ALARM_OFF_OK, OnAlarmOffOk)
	ON_BN_CLICKED(IDB_ALARM_ON_OK, OnAlarmOnOk)
	ON_BN_CLICKED(IDB_BCR_READ_OK, OnBcrReadOk)
	ON_BN_CLICKED(IDB_PORT_LOADING_OK, OnPortLoadingOk)
	ON_BN_CLICKED(IDB_PORT_UNLOADING_OK, OnPortUnloadingOk)
	ON_BN_CLICKED(IDB_READY_TO_WORK_OK, OnReadyToWorkOk)
	ON_BN_CLICKED(IDB_WAFER_WORK_COMPLETE_OK, OnWaferWorkCompleteOk)
	ON_BN_CLICKED(IDB_WAFER_WORK_START_OK, OnWaferWorkStartOk)
	ON_BN_CLICKED(IDB_WORK_COMPLETE_OK, OnWorkCompleteOk)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSettingTCPIPDlg message handlers

void CSettingTCPIPDlg::ButtonInitial()
{

	m_bExit.SetIcon(IDI_EXIT, 32, 32);
	m_bExit.SetImagePos(CPoint(MINBUTTON_CENTER,5));
	m_bExit.SetTextPos(CPoint(MINBUTTON_CENTER,40));
	m_bExit.SetTextSize(10,TRUE);

}

BOOL CSettingTCPIPDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_TimerID = SetTimer(25,100,NULL);
	if( !m_TimerID )
	{
		MyMessageBox("Timer has not been created.","Windows Error", M_ICONERROR);
	}
	ButtonInitial();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSettingTCPIPDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();
}

void CSettingTCPIPDlg::OnAgvOnOk() 
{
	// TODO: Add your control notification handler code here
	m_lnkOnLine->SetAGV(strEqid,TRUE);
	SendMsg();
}

void CSettingTCPIPDlg::OnAgvOffOk() 
{
	// TODO: Add your control notification handler code here
	m_lnkOnLine->SetAGV(strEqid,FALSE);	
	SendMsg();
}

void CSettingTCPIPDlg::OnAlarmOffOk() 
{
	// TODO: Add your control notification handler code here
	m_lnkOnLine->Alarm(strEqid,"Test","Test",FALSE);
	SendMsg();
}

void CSettingTCPIPDlg::OnAlarmOnOk() 
{
	// TODO: Add your control notification handler code here
	m_lnkOnLine->Alarm(strEqid,"Test","Test",TRUE);
	SendMsg();
}

void CSettingTCPIPDlg::OnBcrReadOk() 
{
	// TODO: Add your control notification handler code here
	CString strBCRID;
	strBCRID = m_lnkLoaderPort->GetBCRData();
	m_lnkOnLine->BCRRead(strEqid,strBCRID);
	SendMsg();
}

void CSettingTCPIPDlg::OnPortLoadingOk() 
{
	// TODO: Add your control notification handler code here
	m_lnkOnLine->PortLoading(strEqid);
	SendMsg();
}

void CSettingTCPIPDlg::OnPortUnloadingOk() 
{
	// TODO: Add your control notification handler code here
	m_lnkOnLine->PortUnloading(strEqid);
	SendMsg();
}

void CSettingTCPIPDlg::OnReadyToWorkOk() 
{
	// TODO: Add your control notification handler code here
	CString strBCRID;
	strBCRID = m_lnkLoaderPort->GetBCRData();
	m_lnkOnLine->ReadyToWork(strEqid,strBCRID);
	SendMsg();
}

void CSettingTCPIPDlg::OnWaferWorkCompleteOk() 
{
	// TODO: Add your control notification handler code here
	CString strBCRID;
	strBCRID = m_lnkLoaderPort->GetBCRData();
	m_lnkOnLine->WaferWorkComplete(strEqid,"1","1",strBCRID);
	SendMsg();
}

void CSettingTCPIPDlg::OnWaferWorkStartOk() 
{
	// TODO: Add your control notification handler code here
	CString strBCRID;
	strBCRID = m_lnkLoaderPort->GetBCRData();
	m_lnkOnLine->WaferWorkStart(strEqid,"1","1",strBCRID);
	SendMsg();
}

void CSettingTCPIPDlg::OnWorkCompleteOk() 
{
	// TODO: Add your control notification handler code here
	CString strBCRID;
	strBCRID = m_lnkLoaderPort->GetBCRData();
	m_lnkOnLine->WorkComplete(strEqid,strBCRID);
	SendMsg();
}

void CSettingTCPIPDlg::SendMsg()
{	
	CWnd* pWnd = NULL;
	CString strRecvData;
	strRecvData = m_lnkOnLine->GetSendData();

	if((pWnd = GetDlgItem(IDC_EDIT_COMM_LOG))){
		CString m_eCommLog = _T("");
		
		pWnd->GetWindowText(m_eCommLog);
		
		m_eCommLog += "To : " +strRecvData;
		m_eCommLog += "\r\n";
		
		pWnd->SetWindowText(m_eCommLog);
	}		
}

void CSettingTCPIPDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CDSTapeMountDoc * pDoc = (CDSTapeMountDoc*)((CMainFrame*)AfxGetMainWnd())->GetActiveDocument();
	CString m_strData;
	m_strData = pDoc->m_strReceiveData.Left(pDoc->nRcv-1);
	
	if(pDoc->m_bReceive == TRUE)
	{
		CWnd* pWnd = NULL;
		
		if((pWnd = GetDlgItem(IDC_EDIT_COMM_LOG))){
			CString m_eCommLog = _T("");
			pWnd->GetWindowText(m_eCommLog);
			
			m_eCommLog += "From : " +m_strData;
			m_eCommLog += "\r\n";
			pWnd->SetWindowText(m_eCommLog);
		}
	}
	pDoc->m_bReceive = FALSE;
	CDialog::OnTimer(nIDEvent);
}

